<?php

namespace App\Admin\Controllers;

use App\model\WechatUser;
use App\WechatLink;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WechatLinkController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客户联系链接';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatLink());

        $grid->column('id', __('Id'));
        $grid->column('url', '微信链接');
        $grid->column('name', '名称');
        $grid->column('user_ids','联系人')->display(function ($userIds){
            return WechatUser::whereIn('userid',$userIds)->pluck('name')->implode(',');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatLink::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('url', __('Url'));
        $show->field('name', __('Name'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatLink());

        $form->text('name', '备注')->required();
        $form->multipleSelect('user_ids', '联系人')->options(WechatUser::all()->pluck('name', 'userid'))->required();
        //保存后回调
        $form->saved(function (Form $form) {
            //...
        });
        return $form;
    }
}
